//
//  LoanProcessView.m
//  FinancialStore
//
//  Created by zhenqiu on 16/8/8.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoanProcessView.h"


static id getTitleArray(){
    
    static id titleArray = nil;
    titleArray =@[@"贷款信息",@"个人信息",@"申请完成"];
    return titleArray;
}

static id getImageMetaData(){
    
    static id imageArray = nil;
    
    imageArray = @[@[UI_IMAGE_HOME_LOAN_LOANINF_NORMAL,UI_IMAGE_HOME_LOAN_LOANINF_SELECTED],
                   @[UI_IMAGE_HOME_LOAN_PERINFO_NORMAL,UI_IMAGE_HOME_LOAN_PERINFO_SELECTED],
                   @[UI_IMAGE_HOME_LOAN_APPLYOK_NORMAL,UI_IMAGE_HOME_LOAN_APPLYOK_SELECTED]];
    
    return imageArray;
    
}



static NSInteger statusCount = 3;

@interface LoanProcessView(){
    
    NSMutableArray * lineArray;
    NSMutableArray * titleArray;
    
}

@end

@implementation LoanProcessView
@synthesize imageViewArray;
@synthesize successLabel;

-(instancetype)init{
    
    if(self = [super init]){
        
        self.backgroundColor = [UIColor colorWithString:@"#328dfb"];
//      self.backgroundColor = [UIColor colorWithString:@"#424963"];
        imageViewArray = [NSMutableArray arrayWithCapacity:3];
        lineArray = [NSMutableArray arrayWithCapacity:2];
        titleArray = [NSMutableArray arrayWithCapacity:3];
        
        [self initImageViewArray];
        [self initBottomView];
        [self initLineViews];
        
    }
    
    [self updateConstraintsIfNeeded];
    return self;
    
}

-(void)updateConstraints{
    [super updateConstraints];
    [self updateImageViews];
    [self updateBottomView];
    [self updateLines];
}

#pragma mark -
#pragma mark init subview 

-(void)initImageViewArray{
    
    for (NSInteger i =0; i < statusCount ; i++) {
        
        NSArray * imageArray = getImageMetaData();
        NSString * imageName = [[imageArray objectAtIndex:i] objectAtIndex:0];
        UIView *bgImageView = [[UIView alloc] init];
        bgImageView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
        bgImageView.layer.cornerRadius = LOAN_STATUS_BG_IMAGEVIEW_WIDTH / 2;
        [self addSubview:bgImageView];
        
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:imageName];
        imageView.layer.cornerRadius = LOAN_STATUS_IMAGEVIEW_WIDTH / 2;
        
        [bgImageView addSubview:imageView];
        [imageViewArray addObject:imageView];
        
        UILabel * titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:UI_FOND_TAB_CREDIT_CELL_SUB_1];
        if (i == 0) {
            titleLabel.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];

        }else {
            titleLabel.textColor = [UIColor colorWithWhite:1.0 alpha:0.5];

        }
        titleLabel.text = [getTitleArray() objectAtIndex:i];
        
        [self addSubview:titleLabel];
        [titleArray addObject:titleLabel];
        
    }
    
    
}


-(void)initLineViews{
    
    
    for (NSInteger i =0 ; i < statusCount -1; i++) {
        
        UIImageView * lineView = [[UIImageView alloc] init];
//        lineView.backgroundColor = [UIColor grayColor];
        lineView.image = [UIImage imageNamed:UI_IMAGE_HOME_LOAN_LINK_LINE];
        [self addSubview:lineView];
        [lineArray addObject:lineView];
    }
    
    
}


-(void)initBottomView{
    
    
    UIView *bgBottomView = [[UIView alloc] init];
    bgBottomView.backgroundColor = [UIColor colorWithString:@"#2875d4"];//[UIColor colorWithString:UI_COLOR_CREDIT_BG_1];
    [self addSubview:bgBottomView];
    successLabel = [[UILabel alloc] init];
    successLabel.font = [UIFont systemFontOfSize:UI_FOND_HOME_CREDIT_CELL_SUB_1];
    successLabel.textColor = [UIColor whiteColor];
    successLabel.text = @"贷款申请成功率提高0%";
    [bgBottomView addSubview:successLabel];

}


#pragma mark -
#pragma mark update subview 

-(void)updateBottomView{
    
    UIView * bgBottomView = successLabel.superview;
    
    
    [bgBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
        make.height.mas_equalTo(LOAN_SUCCESS_VIEW_HEIGHT);
        
    }];
    
    [successLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(bgBottomView.mas_centerX);
        make.centerY.equalTo(bgBottomView.mas_centerY);
        
    }];

    
}


-(void)updateImageViews{
    
    id preAttr = self.mas_left;
    
    float width = [UIScreen mainScreen].bounds.size.width;
    NSInteger itemSpace = (width - statusCount * LOAN_STATUS_BG_IMAGEVIEW_WIDTH - LOAN_STATUS_IMAGEVIEW_LEFT * 2)/(statusCount - 1);
    NSInteger leftSpace = LOAN_STATUS_IMAGEVIEW_LEFT;
    
    for (NSInteger i = 0; i < statusCount; i++) {
        
        UIImageView * imageView = [imageViewArray objectAtIndex:i];
        UIView * bgView = imageView.superview;
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(bgView.mas_centerX);
            make.centerY.equalTo(bgView.mas_centerY);
            make.height.mas_equalTo(LOAN_STATUS_IMAGEVIEW_WIDTH);
            make.width.mas_equalTo(LOAN_STATUS_IMAGEVIEW_WIDTH);
            
        }];
        
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.mas_top).with.offset(LOAN_STATUS_IMAGEVIEW_TOP);
            make.left.equalTo(preAttr).with.offset(leftSpace);
            make.width.mas_equalTo(LOAN_STATUS_BG_IMAGEVIEW_WIDTH);
            make.height.mas_equalTo(LOAN_STATUS_BG_IMAGEVIEW_WIDTH);
            
        }];
        
        
        
        
        UILabel * titleLabel = [titleArray objectAtIndex:i];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(bgView.mas_centerX);
            make.top.equalTo(bgView.mas_bottom).with.offset(LOAN_TITLE_LABEL_TOP);
        }];
    
        leftSpace = itemSpace;
        preAttr = bgView.mas_right;

        
    }
    
}

-(void)updateLines{
    
    for (NSInteger i = 0 ; i < [lineArray count] ; i ++) {
        
        UIView * lineView = [lineArray objectAtIndex:i];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            UIView * lView = [[imageViewArray objectAtIndex:i] superview];
            UIView * rView = [[imageViewArray objectAtIndex:i + 1] superview];
            make.height.mas_equalTo(LOAN_LINE_HEIGHT);
            make.left.equalTo(lView.mas_right).with.offset(LOAN_LINE_LEFT);
            make.right.equalTo(rView.mas_left).with.offset(-LOAN_LINE_LEFT);
            make.centerY.equalTo(lView.mas_centerY);
            
        }];
        
    }
    
}


#pragma mark api 

-(void)setProcess:(NSInteger) process{
    
    if(process >= statusCount){
        
        return;
    }
    
    for (NSInteger i = 0; i <= process; i ++) {
        
        NSArray * imageArray = getImageMetaData();
        NSString * imageName = [[imageArray objectAtIndex:i] objectAtIndex:1];
        UIImageView *imageView = [imageViewArray objectAtIndex:i];
        imageView.image = [UIImage imageNamed:imageName];
        UILabel * title = [titleArray objectAtIndex:i];
        title.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    }
    
}


@end
